<?php
namespace Admin\Model;
use Think\Model;
class ArticleModel extends Model{
	//显示文字列表
	public function index(){
		$cates=M('category')->where(array('lock'=>1))->select();
		$cate=class_one_array($cates);//按照父子关系排序好的类别
		//联合搜索功能
		$cid=I('cid','','intval');
		if(isset($cid) && $cid!=null){
		$list=M('category')->where(array('pid'=>$cid))->count();//计算其子模块的个数
		if($list!==0){
			$array=getchilds($cate,$cid);
			$arr=array();
			foreach($array as $v){
				$arr[]=$v['id'];
				$arr[]=$cid;
			}
			$ids=implode(',',$arr);
			//$cid=(string)$cid;
			$where['cid']=array('IN',$ids);
		}
		if($list==0){
			$where['cid']=$cid;
		}
	}
		$where['del']=1;
		$seach=I('seach');
		if(isset($seach)&&$seach!=null){
			$where['_string']="(name like %".$seach."% )or (content like %".$seach."%)";
		}
		//实现分页效果
		$count=$this->where($where)->count();//记录的总数
        $page= new \Think\Page($count,10);   //实例化一个分页类
        $show =$page->show();                //显示分页的样式
        $field=array('id','title','tpic','cid','lock','comment','time','del','name');
        $articles=$this->where($where)->field($field)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        $arr=array();
        foreach($articles as $k=>$v){
        	$v['category']=M('category')->where('id='.$v['cid'])->field('id,name')->find();
        	$arr[]=$v;
        }
        return array(
        	'show' =>$show,
        	'cate' =>$cate,
        	'article'=>$arr
        	);
	}
	//文章添加的数据处理
	public function addDO(){
		if(!empty($_FILES['pic']['name'])){//说明是要上传图片
			$info=$this->upFile();
			if($info['msg']==0){
				echo $info['info'];
			}
			if($info['msg']==1){
				$pic=$info['info']['pic']['savepath'].$info['info']['pic']['savename'];//原图
				$image=new \Think\Image();  //实例化
				$image->open('./'.$pic);
				$thumPic=$info['info']['pic']['savepath'].'thumb_'.$info['info']['pic']['savename'];
                $image->thumb(50,50)->save($thumPic);
                
			}
		}else{
			unset($_POST['pic']);
		}
		     $data=$this->create();
		     if(!empty($_FILES['pic']['name'])){
		     $data['pic']=$pic; //原图地址放入数据库
             $data['tpic']=$thumPic;//缩略图地址放入数据库
		     
		     }
		     $info=$this->add($data);
		     return $info;
		
	}

	//显示编辑页面
	public function edit(){
		$category=M('category')->where(array('lock'=>1))->select();//获得所有的分类
		$category=class_one_array($category);//获得排好序的分类
		$id=I('id');
		$cont=$this->where('id='.$id)->find();
		return array('category'=>$category,'cont'=>$cont);
	
	}

	//处理跟新的数据
	public  function editDo(){
		$id=I('post.id');
		//图片处理
		if(!empty($_FILES['pic']['name'])){
			//说明要跟新图片
			//删除原来的图片
			$oldpic='./'.I('tpic');
			if(file_exists($oldpic)){
				unlink($oldpic);
			}
			//上传新的图片
			$info=$this->upFile();
			if($info['msg']==0){
				echo $info['info'];
			}
			if($info['msg']==1){
				$pic=$info['info']['pic']['savepath'].$info['info']['pic']['savename'];//大图
				$image=new \Think\Image();
				$image->open('./'.$pic);
				$thumPic='./'.$info['info']['pic']['savepath'].'thunb_'.$info['info']['pic']['savename'];
				$image->thumb(50,50)->save($thumPic);
			}
		}
		   $data=$this->create();
		   if(!empty($_FILES['pic']['name'])){
		   	 $data['pic']=$pic; //原图地址放入数据库
             $data['tpic']=$thumPic;//缩略图地址放入数据库
		   }
		   $info=$this->where('id='.$id)->save($data);
		   return $info;
	
}
	//上传的方法
	public  function upFile(){
		$upload=new \Think\Upload();                                //实例化一个上传类
		$upload->maxSize=3*1024*1024;                              //设置上传的最大大小
		$upload->exts=array('jpg','png','jpeg','gif');            //设置上传的允许的后缀
		$upload->rootPath='./';                                   //设置更目录
		$upload->savePath='Uploads/Article/';                     //设置子目录
		$upload->authSub=false;                                
		$upload->saveName='uniqid';                              //文件名的取名方式
		$info=$upload->upload();
		if(!$info){
			$pic=array(
				'msg' =>0,
				'info'=>$upload->getError()
				);
		}else{
			$pic=array(
				'msg'=>1,
				'info'=>$info
				);
		}
		return $pic;
	}

	//显示回收站
	public function  back(){
		$cates=M('category')->where(array('lock'=>1))->select();
		$cate=class_one_array($cates);//按照父子关系排序好的类别
		//联合搜索功能
		$cid=I('cid','','intval');
		if(isset($cid) && $cid!=null){
		$list=M('category')->where(array('pid'=>$cid))->count();//计算其子模块的个数
		if($list!==0){
			$array=getchilds($cate,$cid);
			$arr=array();
			foreach($array as $v){
				$arr[]=$v['id'];
				$arr[]=$cid;
			}
			$ids=implode(',',$arr);
			//$cid=(string)$cid;
			$where['cid']=array('IN',$ids);
		}
		if($list==0){
			$where['cid']=$cid;
		}
	}
		$where['del']=0;
		$seach=I('seach');
		if(isset($seach)&&$seach!=null){
			$where['_string']="(name like %".$seach."% )or (content like %".$seach."%)";
		}
		//实现分页效果
		$count=$this->where($where)->count();//记录的总数
        $page= new \Think\Page($count,10);   //实例化一个分页类
        $show =$page->show();                //显示分页的样式
        $field=array('id','title','tpic','cid','lock','comment','time','del','name');
        $articles=$this->where($where)->field($field)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
        $arr=array();
        foreach($articles as $k=>$v){
        	$v['category']=M('category')->where('id='.$v['cid'])->field('id,name')->find();
        	$arr[]=$v;
        }
        return array(
        	'show' =>$show,
        	'cate' =>$cate,
        	'article'=>$arr
        	);
	}
	//撤销
	public function backdel(){
		$id=I('id');
		$ids=explode(',',$id);
		foreach($ids as $v){
			$data['id']=$v;
			$data['del']=1;
			$this->where('id='.$v)->save($data);
		}
		return true;

	}
   //完全删除记录
	public  function  backdell(){
		$id=I('id');
		$ids=explode(',',$id);//将id转化为一个数组 如1，2，3
		//找到图片的地址
		$urls=array();
		foreach($ids as $key=>$v){
			$pic[$key]=$this->where('id='.$v)->where('pic','tpic')->find();
			$url['pic']=$pic[$key]['pic'];
			$url['tpic']=$pic[$key]['tpic'];
			$urls[]=$url;
		}
		foreach($urls as $v){
			$pic=$v['pic'];
			if(file_exists('./'.$pic)){
				unlink('./'.$pic);
			}
			$tpic=$v['tpic'];
			if(file_exists('./'.$tpic)){
				unlink('./'.$tpic);
			}
		}
		//删除数据
		$where='id in ('.$id.')';
		$this->where($where)->delete();
		return true;
	}
}